import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Rx';
import { HTTP_SERVER_URL, getToken } from '../../app/app.contant';
import 'rxjs/add/operator/map';


@Injectable()
export class ListService {

    constructor(private http: HttpClient) { }

    /**获取分类、明细、子表信息 */
    listCategoryItems() {
        return this.http.get(HTTP_SERVER_URL + 'home/law/material/listgroups?_d_='+new Date()+'&token='+getToken(), { observe: 'response' })
            .map((resp) => resp.body['data'])
            .catch((error: any) => Observable.throw(error || 'Server error'));
    }

    /**获取指定类别下的明细清单 */
    listMaterialsByGroupID(groupID) {
        return this.http.get(HTTP_SERVER_URL + 'home/law/material/listmaterials?_d_='+new Date()+'&token='+getToken()+'&groupID=' + groupID, { observe: 'response' })
            .map((resp) => resp.body['data'])
            .catch((error: any) => Observable.throw(error || 'Server error'));
    }

    /**获取全部已经申报 */
    listMaterialApplies(taskID) {
        return this.http.get(HTTP_SERVER_URL + 'home/law/material/listapplay?_d_='+new Date()+'&token='+getToken()+'&taskID='+taskID, { observe: 'response' })
            .map((resp) => resp.body['data'])
            .catch((error: any) => Observable.throw(error || 'Server error'));
    }

    /** 提交采购申请*/
    submitApplay(applies) {
        return this.http.post(HTTP_SERVER_URL + 'home/law/material/submitapply?_d_='+new Date()+'&token='+getToken(), applies, { observe: 'response' })
            .map((resp) => resp.body['data'])
            .catch((error: any) => Observable.throw(error || 'Server error'));
    }
}